package com.open.capacity.nk.service.impl;

import com.open.capacity.db.config.util.DataSourceHolder;
import com.open.capacity.db.config.util.DataSourceKey;
import com.open.capacity.nk.constant.NKDataSourceKeys;
import com.open.capacity.nk.entity.NkApprovalpaymentreceipt;
import com.open.capacity.nk.entity.NkDirectpaymentreceipt;
import com.open.capacity.nk.entity.NkDirectpaymentreceipt;
import com.open.capacity.vo.TreeVo;
import javafx.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.nk.entity.NkDirectpaymentreceipt;
import com.open.capacity.nk.dao.NkDirectpaymentreceiptDao;
import com.open.capacity.nk.service.NkDirectpaymentreceiptService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class NkDirectpaymentreceiptServiceImpl implements NkDirectpaymentreceiptService {

    @Autowired
    private NkDirectpaymentreceiptDao receiptDao;

    @Override
    public int syncData() {
        Map<String, Object> params = new HashMap<>();
        List<NkDirectpaymentreceipt> list = new ArrayList<>();

        //本院
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk00);
        List<NkDirectpaymentreceipt> list0 = receiptDao.findAll(params);
        list0.forEach(t->t.setIdorganization(NKDataSourceKeys.CENTER.getCode()));
        list.addAll(list0);

        //天河
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk01);
        List<NkDirectpaymentreceipt> list1 = receiptDao.findAll(params);
        list1.forEach(t->t.setIdorganization(NKDataSourceKeys.TH.getCode()));
        list.addAll(list1);

        //海珠
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk02);
        List<NkDirectpaymentreceipt> list2 = receiptDao.findAll(params);
        list2.forEach(t->t.setIdorganization(NKDataSourceKeys.HZ.getCode()));
        list.addAll(list2);

        //越秀
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk03);
        List<NkDirectpaymentreceipt> list3 = receiptDao.findAll(params);
        list3.forEach(t->t.setIdorganization(NKDataSourceKeys.YX.getCode()));
        list.addAll(list3);

        //荔湾
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk04);
        List<NkDirectpaymentreceipt> list4 = receiptDao.findAll(params);
        list4.forEach(t->t.setIdorganization(NKDataSourceKeys.LW.getCode()));
        list.addAll(list4);

        //白云
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk05);
        List<NkDirectpaymentreceipt> list5 = receiptDao.findAll(params);
        list5.forEach(t->t.setIdorganization(NKDataSourceKeys.BY.getCode()));
        list.addAll(list5);

        //番禺
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk06);
        List<NkDirectpaymentreceipt> list6 = receiptDao.findAll(params);
        list6.forEach(t->t.setIdorganization(NKDataSourceKeys.PY.getCode()));
        list.addAll(list6);

        //黄埔
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk07);
        List<NkDirectpaymentreceipt> list7 = receiptDao.findAll(params);
        list7.forEach(t->t.setIdorganization(NKDataSourceKeys.HP.getCode()));
        list.addAll(list7);

        //南沙
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk08);
        List<NkDirectpaymentreceipt> list8 = receiptDao.findAll(params);
        list8.forEach(t->t.setIdorganization(NKDataSourceKeys.NS.getCode()));
        list.addAll(list8);

        //花都
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk09);
        List<NkDirectpaymentreceipt> list9 = receiptDao.findAll(params);
        list9.forEach(t->t.setIdorganization(NKDataSourceKeys.HD.getCode()));
        list.addAll(list9);

        //增城
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk10);
        List<NkDirectpaymentreceipt> list10 = receiptDao.findAll(params);
        list10.forEach(t->t.setIdorganization(NKDataSourceKeys.ZC.getCode()));
        list.addAll(list10);

        //从化
        DataSourceHolder.setDataSourceKey(DataSourceKey.nk11);
        List<NkDirectpaymentreceipt> list11 = receiptDao.findAll(params);
        list11.forEach(t->t.setIdorganization(NKDataSourceKeys.CH.getCode()));
        list.addAll(list11);

        DataSourceHolder.clearDataSourceKey();

        //本地存库
        receiptDao.delete();
        receiptDao.batchSave(list);
//        list.forEach(t -> {
//            receiptDao.save(t);
//        });

        return list.size();
    }

    /**
     * 添加
     *
     * @param nkDirectpaymentreceipt
     */
    public int save(NkDirectpaymentreceipt nkDirectpaymentreceipt) {
        return receiptDao.save(nkDirectpaymentreceipt);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return receiptDao.delete();
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public NkDirectpaymentreceipt findById(String id) {
        return receiptDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<NkDirectpaymentreceipt> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<NkDirectpaymentreceipt> list = receiptDao.findAll(params);
        PageInfo<NkDirectpaymentreceipt> pageInfo = new PageInfo(list);

        return PageResult.<NkDirectpaymentreceipt>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<NkDirectpaymentreceipt> findAll(Map<String, Object> params) {
        return receiptDao.findAll(params);
    }

    @Override
    public List<NkDirectpaymentreceipt> findDepartmentData(Map<String, Object> params) {
        return receiptDao.findDepartmentData(params);
    }

    @Override
    public List<TreeVo> findOrganization(Map<String, Object> params) {
        return receiptDao.findOrganization();
    }

}
